<script>

//***********************************************************************************************************************
//set delivered qty to max and submit form
function AllProductDelivered()
{
        <?php
                $collection = $this->getOrder()->getProducts();
                foreach ($collection as $item)
                {
                    $remaining_qty = $item->getRemainingQty();
                    if ($remaining_qty > 0)
                            echo "document.getElementById('add_sm_qty_".$item->getpop_product_id()."').value = ".$remaining_qty.";";
                }
        ?>
}

//***************************************************************************************************************************
//display serials count
function displaySerialsCount(id)
{
	var serials = document.getElementById('serials_' + id).value;
	var t_serials = serials.split("\n");
	var nb = 0;
	var i;
	for(i=0;i<t_serials.length;i++)
	{
		if (t_serials[i] != '')
			nb++;
	}
	document.getElementById('serials_count_' + id).innerHTML = '  ' + nb + 'x';
	
}

</script>



<div class="entry-edit-head">
	<h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__('Create New delivery'); ?></h4> 
</div>
<div class="entry-edit">
        <fieldset id="my-fieldset">
			<table border="0" width="100%">
				<tr>
					<td><?php echo $this->__('Date'); ?></td>
					<td width="30">&nbsp;</td>
					<td>
						<input type="text" id="add_sm_date" name="add_sm_date" value="<?php echo date('Y-m-d'); ?>">
	                	<img src="<?php echo $this->getSkinUrl('images/grid-cal.gif') ?>" class="v-middle" id="img_calendar_sm" />
				        <script type="text/javascript">
			                Calendar.setup({
			                    inputField : 'add_sm_date',
			                    ifFormat : '%Y-%m-%e',
			                    button : 'img_calendar_sm',
			                    align : 'Bl',
			                    singleClick : true
			                });
		                </script>	
					</td>
					<td rowspan="2" class="a-center">
						<p><button onclick="document.location.href='<?php echo $this->getBarcodeLabelsUrl(); ?>';" class="scalable a-right" type="button"><span><?php echo $this->__('Print barcode sheet')?></span></button></p>
						<p><button onclick="AllProductDelivered();" class="scalable a-right" type="button"><span><?php echo $this->__('All products delivered')?></span></button></p>
					</td>
				</tr>
				<tr>
					<td><?php echo $this->__('Target warehouse'); ?></td>
					<td width="30">&nbsp;</td>
					<td><?php echo $this->getWarehousesAsCombo('add_sm_warehouse_id'); ?></td>
				</tr>
			</table>
			<p>&nbsp;</p>
			<div class="grid">
				<table border="0"  cellspacing="0" class="data" width="100%" cellspacing="0">
		        	<thead>
						<tr class="headings">
							<?php if (mage::getStoreConfig('purchase/purchase_order/display_product_picture') == 1): ?>
								<th class="a-center"><?php echo $this->__('Picture'); ?></th>
							<?php endif; ?>
							<th class="a-center"><?php echo $this->__('Sku'); ?></th>
							<th class="a-center"><?php echo $this->__('Product'); ?></th>
							<th class="a-center"><?php echo $this->__('Qty'); ?></th>
                                                        <?php if (mage::helper('purchase/Product_Packaging')->isEnabled()): ?>
                                                                <th class="a-center"><?php echo $this->__('Packaging') ?></th>
                                                        <?php endif; ?>
							<th class="a-center"><?php echo $this->__('Ordered Qty'); ?></th>
							<th class="a-center"><?php echo $this->__('Already Delivered Qty'); ?></th>
							<th class="a-center"><?php echo $this->__('Remaining Qty'); ?></th>
							<th class="a-center"><?php echo $this->__('Barcode'); ?></th>
							<th class="a-center"><?php echo $this->__('Serials'); ?></th>
							<th class="a-center"><?php echo $this->__('Location'); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php
							$collection = $this->getOrder()->getProducts();
							foreach ($collection as $item)
							{
                                                            
								if ($item->getpop_qty() > $item->getpop_supplied_qty())
								{
									$remaining_qty = $item->getRemainingQty();
									echo '<tr>';
									
									//add product picture
									if (mage::getStoreConfig('purchase/purchase_order/display_product_picture') == 1)
									{
										if ($item->getsmall_image() != 'no_selection')
										{
											$productImage = Mage::getBaseUrl('media').'catalog/product'.$item->getsmall_image(); //mage::helper('catalog/image')->init($product, 'small_image')->resize(50, 50);										
											echo '<td class="a-center"><img src="'.$productImage.'" width="50" height="50"></td>';
										}
										else 
											echo '<td>&nbsp;</td>';
									}
																			
									//display sku
									echo '<td><a href="'.$this->getUrl('AdvancedStock/Products/Edit', array('product_id' => $item->getpop_product_id())).'">'.$item->getsku();
									if ($item->getpop_supplier_ref() != '')
										echo '<br>('.$item->getpop_supplier_ref().')';
									echo '</a></td>';
										
									echo '<td>'.$item->getpop_product_name().'</td>';
									//Si on doit bloquer les qte recues (cad qu'elles ne soient pas sup�rieure � la qte command�e
									if ((mage::getStoreConfig('purchase/purchase_order/check_delivery_qty') == 1) && ($remaining_qty < 100))
									{
										echo '<td class="a-center">';
										echo '<select id="add_sm_qty_'.$item->getpop_product_id().'"  name="add_sm_qty_'.$item->getpop_product_id().'">';
										for ($i=0;$i<=$remaining_qty;$i++)
											echo '<option value="'.$i.'">'.$i.'</option>';
										echo '</select>';
										echo '</td>';										
									}
									else 
									{
										echo '<td class="a-center"><input type="text" id="add_sm_qty_'.$item->getpop_product_id().'" name="add_sm_qty_'.$item->getpop_product_id().'" value="0" size="5"></td>';									
									}
									

                                                                        if (mage::helper('purchase/Product_Packaging')->isEnabled())
                                                                        {
                                                                            echo '<td>'.$item->getpop_packaging_name().' ('.$item->getpop_packaging_value().'x)</td>';
                                                                        }

                                                                        echo '<td class="a-center">'.$item->getOrderedQty().'</td>';
									echo '<td class="a-center">'.$item->getSuppliedQty().'</td>';
									echo '<td class="a-center">'.$remaining_qty.'</td>';
									
									echo '<td class="a-center">'.mage::helper('AdvancedStock/Product_Barcode')->getBarcodeForProduct($item->getpop_product_id()).'<br><input type="text" id="barcode_'.$item->getpop_product_id().'" name="barcode_'.$item->getpop_product_id().'" value="" size="13"></td>';
									echo '<td class="a-center">';
									echo '<textarea onkeyup="displaySerialsCount('.$item->getpop_product_id().');" name="serials_'.$item->getpop_product_id().'" id="serials_'.$item->getpop_product_id().'" cols="30" rows="3"></textarea>';
									echo '<span id="serials_count_'.$item->getpop_product_id().'" name="serials_count_'.$item->getpop_product_id().'"> 0x</span></td>';
									
									echo '<td class="a-center"><input type="text" id="location_'.$item->getpop_product_id().'" name="location_'.$item->getpop_product_id().'" value="'.$this->getProductLocation($item->getpop_product_id()).'" size="5"></td>';
									
									echo '</tr>';
								}
							}
						?>
					</tbody>
				</table>
			</div>
		</fieldset>
</div>


<div class="entry-edit-head">
	<h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__('Deliveries'); ?></h4> 
</div>
<div class="entry-edit">
        <div class="grid">
            <table cellspacing="0" class="data" width="100%">
	        	<thead>
					<tr class="headings">
		            	<th class="a-center"><?php echo $this->__('Date'); ?></th>
		            	<th><?php echo $this->__('Product'); ?></th>
		            	<th class="a-center"><?php echo $this->__('Warehouse'); ?></th>
		            	<th class="a-center"><?php echo $this->__('Qty'); ?></th>
		            	<th><?php echo $this->__('Description'); ?></th>
		            </tr>
		         </thead>
		         <tbody>
					<?php
						//Affiche les mouvements de stock
						$collection = $this->getOrder()->getStockMovements();
						foreach ($collection as $item)
						{
							echo '<tr>';
							echo '<td class="a-center">'.$item->getsm_date().'</td>';
							echo '<td><a href="'.$this->getUrl('AdvancedStock/Products/Edit', array('product_id' => $item->getProduct()->getId())).'">'.$item->getProduct()->getname().'</a></td>';
							echo '<td>'.$item->getTargetWarehouseName()->getstock_name().'</td>';
							echo '<td class="a-center">'.$item->getsm_qty().'</td>';
							echo '<td>'.$item->getsm_description().'</td>';
							echo '</tr>';
						}
					
					?>
				</tbody>
			</table>
		</div>
</div>